﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AnaViewer
{
    /// <summary>
    /// <para>透明窗体。你可以通过SetBitmap设置要显示的图像。</para>
    /// <para>
    /// 在将要设置的快捷菜单中，如果包含一项（可以是子菜单）Text 为 ReplaceMenuText，
    /// 则该项将被默认的工具菜单代替。否则，将添加到菜单的末尾。
    /// </para>
    /// </summary>
    public partial class AlphaForm : Form
    {
        Bitmap bitmap;
        public void SetBitmap(Bitmap bmp)
        {
            bitmap = bmp;
            Common.SetBitmap(this, bitmap, opacity);
        }
        public void SetBitmap(Bitmap bmp,byte opacity)
        {
            this.bitmap = bmp;
            this.opacity = opacity;
            Common.SetBitmap(this, bitmap, opacity);
        }
        //FormDrag drager;

        /// <summary>
        /// 初始化类 <see cref="AlphaForm"/> 的新实例。
        /// </summary>
        public AlphaForm()
        {
            InitializeComponent();
            FormBorderStyle = FormBorderStyle.None;

            TopMost = true;
            ShowInTaskbar = false;
            //drager = new FormDrag(this);
            //drager.FormLocked = false;
        }

        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams cp = base.CreateParams;
        //        cp.ExStyle |= 0x00080000; // This form has to have the WS_EX_LAYERED extended style
        //        return cp;
        //    }
        //}

        ///// <summary>
        ///// Gets or sets 一个值，指示是否 [锁定窗体位置]。
        ///// </summary>
        ///// <value>如果 [form locked]为 <c>true</c>；否则为 <c>false</c>。</value>
        //public bool FormLocked
        //{
        //    get { return drager.FormLocked; }
        //    set { drager.FormLocked = value; }
        //}
        private bool embedDesktop = false;
        /// <summary>
        /// Gets or sets 一个值，指示是否 [嵌入到桌面]。
        /// </summary>
        /// <value>如果 [embed desktop]为 <c>true</c>；否则为 <c>false</c>。</value>
        public bool EmbedDesktop
        {
            get { return embedDesktop; }
            set
            {
                embedDesktop = value;
                Common.EmbedDesktop(this, embedDesktop);
            }
        }
        private bool canPenetrate = false;
        /// <summary>
        /// Gets or sets 一个值，只是该实例是否为 鼠标穿透。
        /// </summary>
        /// <value>如果该实例can penetrate，为<c>true</c>；否则为<c>false</c>。</value>
        public bool CanPenetrate
        {
            get { return canPenetrate; }
            set
            {
                canPenetrate = value;
                Common.CanPenetrate(this, canPenetrate);
            }
        }


        private byte opacity = 255;
        /// <summary>
        /// Gets or sets 透明度的百分比
        /// </summary>
        /// <value>The percent.</value>
        public int OpacityPercent
        {
            get { return opacity * 100 / 255; }
            set
            {
                opacity = (byte)(255 * value / 100);
                if (bitmap != null)
                    Common.SetBitmap(this, bitmap, opacity);
            }
        }

        private Region closeBtnRegion = null;
        /// <summary>
        /// 关闭按钮的区域。鼠标点击该区域将导致关闭窗体。
        /// </summary>
        protected Region CloseButtonRegion
        {
            get
            {
                return closeBtnRegion;
            }
            set
            {
                closeBtnRegion = value;
            }
        }
        private Region minBtnRegion = null;
        /// <summary>
        /// 最小化按钮的区域。鼠标点击该区域将导致窗体最小化。
        /// </summary>
        protected Region MinButtonRegion
        {
            get
            {
                return minBtnRegion;
            }
            set
            {
                minBtnRegion = value;
            }
        }
        protected override void OnMouseClick(MouseEventArgs e)
        {
            //如果设置了关闭区域，且点击在关闭区域内，则关闭窗体
            if (closeBtnRegion != null && closeBtnRegion.IsVisible(e.Location))
                this.Close();
            else if (minBtnRegion != null && minBtnRegion.IsVisible(e.Location))
                this.WindowState = FormWindowState.Minimized;

            base.OnMouseClick(e);
        }

    }
}